package com.guli.gulikiller.killer.controller;

import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import com.google.common.util.concurrent.RateLimiter;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.R;
import com.guli.gulikiller.killer.entity.StockOrderEntity;
import com.guli.gulikiller.killer.service.StockOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.bytebuddy.implementation.bytecode.Throw;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2022-05-27 09:51:41
 */
@RestController
@RequestMapping("generator/stockorder")
@Api("")
public class StockOrderController {
    @Autowired
    private StockOrderService stockOrderService;
    //每秒放行50个请求
    private  RateLimiter rateLimiter = RateLimiter.create(10);
    /**
     * 列表
     */
    @PostMapping("/list")
    //@RequiresPermissions("generator:stockorder:list")
    @ApiOperation("查询")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = stockOrderService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @PostMapping("/info/{id}")
   // @RequiresPermissions("generator:stockorder:info")
    @ApiOperation("根据id查询")
    public R info(@PathVariable("id") Integer id){
		StockOrderEntity stockOrder = stockOrderService.getById(id);

        return R.ok().put("stockOrder", stockOrder);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    //@RequiresPermissions("generator:stockorder:save")
    @ApiOperation("保存")
    public R save(@RequestBody StockOrderEntity stockOrder){
		stockOrderService.save(stockOrder);

        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation("修改")
    //@RequiresPermissions("generator:stockorder:update")
    public R update(@RequestBody StockOrderEntity stockOrder){
		stockOrderService.updateById(stockOrder);

        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
   // @RequiresPermissions("generator:stockorder:delete")
    @ApiOperation("删除")
    public R delete(@RequestBody Integer[] ids){
		stockOrderService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @PostMapping("/createWrongOrder/{sid}")
    @ApiOperation(value = "创造错误订单",notes = "查询库存，有相应库存，则创建订单，并扣除库存")
    public R createWrongOrder(@PathVariable Integer sid) {
        System.out.println("购买物品编号sid="+sid);
        int id = 0;
        try{
            id = stockOrderService.createWrongOrder(sid);
            System.out.println("创建订单id: "+id);
        }catch (Exception e){
            System.out.println("EXCEPTION:"+e);
        }
        return id!=0?R.ok().put("data",id):R.error().put("data","库存不足");
    }
    @PostMapping("/createSadOrder/{sid}")
    @ApiOperation(value = "使用悲观锁创造错误订单",notes = "查询库存，有相应库存，则创建订单，并扣除库存")
    public R createSadOrder(@PathVariable Integer sid) {
        System.out.println("购买物品编号sid="+sid);
        int id = 0;
        try{
            id = stockOrderService.createSadOrder(sid);
            System.out.println("创建订单id: "+id);
        }catch (Exception e){
            System.out.println("EXCEPTION:"+e);
        }
        return id!=0?R.ok().put("data",id):R.error().put("data","库存不足");
    }
    @PostMapping("/createOptimisticOrder/{sid}")
    @ApiOperation(value = "基于乐观锁创造订单",notes = "查询库存，有相应库存，则创建订单，并扣除库存")
    public R createOptimisticOrder(@PathVariable Integer sid) {
        if(!rateLimiter.tryAcquire(10, TimeUnit.MILLISECONDS)){
            System.out.println("you are throttled");
            throw  new  RuntimeException("you are throttled");

        }
        System.out.println("购买物品编号sid="+sid);
        int id = 0;
        try{
            id = stockOrderService.createOptimisticOrder(sid);
            System.out.println("创建订单id: "+id);
        }catch (Exception e){
            System.out.println("EXCEPTION:"+e);
        }
        return id!=0?R.ok().put("data",id):R.error().put("data","stock is empty");
    }
}
